<?php
ob_start();
include("../lib/config.php");
include("../lib/var.php");
include("../lib/function.php");
include("../lib/header.php");

if(isset($_GET['delete'])){
	mssql_query("DELETE FROM Jurusan WHERE id='".$_GET['id']."'");
}

if(isset($_POST['Submit']) && $_POST['id']!=""){
	//Edit
	mssql_query("UPDATE Jurusan SET nama='".$_POST['jurusan']."',fakultas_id='".$_POST['fakultas']."' WHERE id='".$_POST['id']."'");
}elseif(isset($_POST['Submit']) && $_POST['id']==""){
	//Add
	mssql_query("INSERT INTO Jurusan (nama,fakultas_id) VALUES('".$_POST['jurusan']."','".$_POST['fakultas']."')");
}

if(isset($_GET['edit'])){
	@list($nama) = mssql_fetch_row(mssql_query("SELECT nama FROM Jurusan WHERE id='".$_GET['id']."'"));
}
?>
<link rel="stylesheet" type="text/css" href="<?=$path?>/style/style.css" />
<script language="javascript" src="<?=$path?>/js/event.js"></script>
<script language="javascript" src="<?=$path?>/js/calendar.js"></script>
<script language="javascript" src="<?=$path?>/js/jquery.min.js"></script>
<script language="javascript" src="<?=$path?>/js/apps.js"></script>
<script language="javascript">
function Validation(){
	if ($("#fakultas").val() == ''){
		alert('Fakultas Harus diisi');
		document.FrmFakultas.fakultas.focus();
		return false;
	}else if($("#jurusan").val() == ''){
		alert('Jurusan Harus diisi');
		document.FrmFakultas.jurusan.focus();
		return false;
	}else{
		return true;
	}
}
</script>
<strong style="background-color: rgb(0, 51, 153); color: rgb(255, 255, 255); padding:2 2 2 2;">FAKULTAS PENDIDIKAN</strong>
<hr color="#FFFFFF" noshade>
<body bgcolor="#FFFFFF">
<form method="post" name="FrmFakultas" onSubmit="return Validation()">
<table>
    <tr>
        <td width="100"><strong>FAKULTAS</strong></td>
        <td>:<select name="fakultas" id="fakultas">
        <option value="">Pilih Fakultas</option>
        <?php
		$jenjang = mssql_query("SELECT * FROM Fakultas");
		while($dtJenjang = mssql_fetch_array($jenjang)){
			if(isset($_GET[fak]) && $_GET[fak] == $dtJenjang[id]){
            	echo "<option value=\"$dtJenjang[id]\" selected>$dtJenjang[nama]</option>";
			}
			echo "<option value=\"$dtJenjang[id]\">$dtJenjang[nama]</option>";
		}
		?>
            <span class="required">*</span>
        </select>
        </td>
        <td align="right"></td>
    </tr>
    <tr>
        <td width="100"><strong>JURUSAN</strong></td>
        <td>
            :<input class="text" id="jurusan" type="text" name="jurusan" style="font-weight: bold;" value="<?php if(isset($_GET['edit'])) echo $nama; ?>"/>
            <input type="hidden" name="id" value="<?php if(isset($_GET['edit'])) echo $_GET['id']; ?>"/>
            <span class="required">*</span>
        </td>
        <td align="right"><input class="button" name="Clear" type="button" onClick="javascript:window.location='frm_add_jurusan.php'" value="Clear"/><input class="button" name="Submit" type="submit" value="Save"/></td>
    </tr>
</table>
</form>
<?php
	$result	= mssql_query("SELECT * FROM Jurusan",$conn);
	$total	= mssql_num_rows($result);
	$limit	= 5;
	$page 	= $HTTP_GET_VARS['page']; // get nomor halaman yang dipanggil
	$pager  = Pager::getPagerData($total, $limit, $page);
	$offset = $pager->offset;
	$limit  = $pager->limit;
	$page   = $pager->page;
	$sql	= "WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY jr.id) AS no,jr.id as jrid,f.id as fid,f.nama as fnama,jr.nama as jrnama FROM Jurusan jr left join Fakultas f on jr.fakultas_id=f.id) SELECT * FROM limit WHERE no BETWEEN ".$offset." AND ".($offset+$limit).""; 
	//echo $sql;
	Paging1($pager,$page,$next,$limit,$keyword,$show);

?>
<table width="100%">
    <tr class=headlist>
        <td width=2%>No.</td>
        <td width=15%>Fakultas</td>
        <td width=15%>Jenjang</td>
        <td width=7%>Manage</td>
    </tr>

<?php
	$dJenjang = mssql_query($sql,$conn);
	while($dtJ = mssql_fetch_array($dJenjang)){
		echo "
		<tr class=contentlist>
			<td width=2% align=center>$dtJ[no]</td>
			<td width=15%>$dtJ[fnama]</td>
			<td width=15%><a href=\"frm_add_jurusan.php?edit&fak=$dtJ[fid]&id=$dtJ[jrid]\">$dtJ[jrnama]</a></td>
			<td width=2% align=center>
				<a href=\"frm_add_jurusan.php?edit&fak=$dtJ[fid]&id=$dtJ[jrid]\">
				<img border=\"0\" alt=\"Edit\" title=\"Edit\" src=\"../images/b_edit.png\"/>
				</a>
				<a href=\"frm_add_jurusan.php?delete&id=$dtJ[jrid]\">
				<img border=\"0\" alt=\"Delete\" title=\"Delete\" src=\"../images/b_delete.png\"/>
				</a>			
			</td>
		</tr>		
		";
	}
?>

</body>